#import "BWColorInspector.h"

#import "BWColorsetDocument.h"
#import "BWThreadColor.h"

static BWColorInspector *g_inspector;


@implementation BWColorInspector

+ (BWColorInspector *) defaultInspector
{
    if (g_inspector == nil) {
        g_inspector = [[BWColorInspector alloc] init];
    }

    if (![[g_inspector window] isVisible]) {
        [g_inspector showWindow: self];
    }

    return (g_inspector);

} // defaultInspector



- (id) init
{
    if (self = [super initWithWindowNibName: @"InspectorWindow"]) {
        
    }

    return (self);

} // init


- (void) updateUI
{
    if (currentColor == nil) {
        [idField setStringValue: @""];
        [nameField setStringValue: @""];

    } else {
        NSString *value;

        value = [currentColor manufacturerID];
        value = (value) ? value : @"";

        [idField setStringValue: value];

        if ([currentColor color] != nil) {
            [colorWell setColor: [currentColor color]];
        }

        value = [currentColor name];
        value = (value) ? value : @"";

        [nameField setStringValue: value];
    }

} // updateUI


- (void) setCurrentDocument: (BWColorsetDocument *) document
{
    [document retain];
    [currentDocument release];

    [self setCurrentColor: nil];

    currentDocument = document;

} // setCurrentDocument


- (void) setCurrentColor: (BWThreadColor *) color
{
    [color retain];
    [currentColor release];

    currentColor = color;

    [self updateUI];

} // setCurrentColor


- (IBAction) apply: (id) sender
{
    [currentColor setManufacturerID: [idField stringValue]];
    [currentColor setColor: [colorWell color]];
    [currentColor setName: [nameField stringValue]];

    [currentDocument colorChanged: currentColor];

} // apply


@end // BWColorInspector


